﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Drawing;

namespace DesignPanel.Service
{
    public delegate void HookContextMenu(Point ptMouse);

    public class MenuService : IMenuCommandService, IDisposable
    {
        internal event HookContextMenu OnContextMenu = null;

        //- this ServiceProvider is the DesignsurfaceExt2 instance 
        //- passed as paramter inside the ctor
        private IServiceProvider m_serviceProvider = null;
        private MenuCommandService m_menuCommandService = null;

        #region IMenuCommandService 成员

        public MenuService(IServiceProvider serviceProvider)
        {
            m_serviceProvider = serviceProvider;
            m_menuCommandService = new MenuCommandService(serviceProvider);
        }

        public void AddCommand(MenuCommand command)
        {
            m_menuCommandService.AddCommand(command);
        }

        public void AddVerb(DesignerVerb verb)
        {
            m_menuCommandService.AddVerb(verb);
        }

        public MenuCommand FindCommand(CommandID commandID)
        {
            return m_menuCommandService.FindCommand(commandID);
        }

        public bool GlobalInvoke(CommandID commandID)
        {
            MenuCommand command = FindCommand(commandID);
            if ((command != null) && (command.Supported))
            {
                //command.Enabled = true;
                //command.Checked = true;
                //command.Visible = true;
                command.Invoke();
                return true;
            }
            return m_menuCommandService.GlobalInvoke(commandID);
        }

        public void RemoveCommand(MenuCommand command)
        {
            m_menuCommandService.RemoveCommand(command);
        }

        public void RemoveVerb(DesignerVerb verb)
        {
            m_menuCommandService.RemoveVerb(verb);
        }

        public void ShowContextMenu(CommandID menuID, int x, int y)
        {
            if ((OnContextMenu != null))
            {
                Point ptMouse = new Point(x, y);
                OnContextMenu(ptMouse);
            }
        }

        public DesignerVerbCollection Verbs
        {
            get
            {
                return m_menuCommandService.Verbs;
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            m_menuCommandService.Dispose();
            m_menuCommandService = null;
        }

        #endregion
    }
}
